package com.ymttest.database.sqlwapper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.common.entitie.TimeRange;
import com.ymttest.common.enums.DateTypeEnum;
import com.ymttest.common.enums.OrderFilterEnum;
import com.ymttest.common.enums.OrderStatusEnum;
import com.ymttest.database.model.YmtOrderExt;
import com.ymttest.database.model.YmtOrderInfo;
import com.ymttest.database.model.YmtOrders;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

/************************************************************************************
 * @File name : YmtOrderInfoWapper.java
 * @Copyright : 洋码头
 ************************************************************************************/
public class YmtOrderInfoWapper {
	SqlManager manage;

	public YmtOrderInfoWapper() {
		manage = new SqlManager("ymt" + EnvSetup.getEnv());
	}

	
	public List<YmtOrderInfo> selectByOrderId(int orderId) {
		Map m = new HashMap();
		m.put("orderId", orderId);
		return (List<YmtOrderInfo>) manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtOrderInfoMapper.selectByOrderId",
						m);
	}
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}


	
	/**
	 * 
	 * @param queryField 查询的字段
	 * @param joinCondition 连表查询
	 * @param queryCondition 查询条件
	 * @return
	 */
	public List<Map> selectOrders2(String queryField, String joinCondition, String queryCondition) {
		Map m = new HashMap();
		if(null != queryField && !"".equals(queryField)){
			m.put("queryField", queryField);
		}
		if(null != joinCondition && !"".equals(joinCondition)){
			m.put("joinCondition", joinCondition);
		}
		if(null != queryCondition && !"".equals(queryCondition)){
			m.put("queryCondition", queryCondition);
		}
		return (List<Map>) manage.getSelectResults(
				"com.ymttest.database.mapping.YmtOrdersMapper.selectOrders2", m);
	}

	@SuppressWarnings("rawtypes")
	public List<Map> selectAppSellerOrdersTestData(Integer queryCount,
			String queryCondition) {
		Map m = new HashMap();
		m.put("queryCondition", queryCondition);
		m.put("queryCount", queryCount);
		return (List<Map>) manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtOrdersMapper.selectAppSellerOrdersTestData",
						m);
	}
	
	@SuppressWarnings("rawtypes")
	public List<Map> selectAppSellerOrdersTestData(Integer queryCount,
			String queryCondition,String joinCondition) {
		Map m = new HashMap();
		m.put("queryCondition", queryCondition);
		m.put("queryCount", queryCount);
		m.put("joinCondition", joinCondition);
		return (List<Map>) manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtOrdersMapper.selectAppSellerOrdersTestData",
						m);
	}

	@SuppressWarnings("rawtypes")
	public List<Map> selectAppSellerOrders(List<Integer> DateList,
			Map<Integer, String> Filter, Integer LastOrderId, Integer OrderNum,
			List<Integer> OrderStatusList, Integer SellerUserId)
			throws InterruptedException, ParseException {
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("iOrderId", LastOrderId);
		m.put("num", OrderNum);
		m.put("iBuyerId", SellerUserId);
		m.put("iTradingStatus", OrderStatusList);
		// DateList
		TimeRange timeRange = TimeRange.getTimeRangeByDateTypeEnum(DateList);
		if (null != timeRange) {
			m.put("timeFrom", timeRange.getStartTime()); // timeFrom为今年的1月1号
			m.put("timeTo", timeRange.getEndTime());// timeTo为明年的1月1号
		}

		// Filter 四个查询条件在前端app不会并列出现
		Iterator<Integer> it = Filter.keySet().iterator();
		StringBuffer filterSB = new StringBuffer();
		for (; it.hasNext();) {
			Integer filter = it.next();
			String value = Filter.get(filter);
			switch (filter) {
			case 1:
				if (filterSB.length() > 0) {
					filterSB.append(" or ");
				}
				filterSB.append("o.iOrderId like CONCAT('%','" + value
						+ "','%' )");
				break;
			case 2:
				if (filterSB.length() > 0) {
					filterSB.append(" or ");
				}
				filterSB.append("o.sBuyerNickName like CONCAT('%','" + value
						+ "','%' )");
				break;
			case 3:
				if (filterSB.length() > 0) {
					filterSB.append(" or ");
				}
				filterSB.append("o.sReceivePerson like CONCAT('%','" + value
						+ "','%' )");
				break;
			case 4:
				if (filterSB.length() > 0) {
					filterSB.append(" or ");
				}
				filterSB.append("exists(select top 1 1 from Ymt_OrderSummary(nolock) where iOrderId = o.iOrderId and sSummary like CONCAT('%','"
						+ value + "','%' ))");
				break;
			}
		}
		if (filterSB.length() > 0) {
			m.put("filter", filterSB.toString());
		}

		// Filter
		List<Map> orders = (List<Map>) manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtOrdersMapper.selectAppSellerOrders",
						m);
		manage.commit();
		// manage.close();
		return orders;
	}

	@SuppressWarnings("rawtypes")
	public List<YmtOrders> selectbyMainOrder(int mainOrderId) {
		Map m = new HashMap();
		m.put("mainOrderId", mainOrderId);
		return (List<YmtOrders>) manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtOrdersMapper.selectbyMainOrder",
						m);
	}
	
	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String args[]) throws InterruptedException {
		// List<Integer> DateList = new ArrayList<Integer>();
		// DateList.add(DateTypeEnum.Yesterday.getValue());
		// Map<Integer, String> Filter = new HashMap<Integer, String>();
		// Filter.put(OrderFilterEnum.ConsigneeName.getValue(), "多");
		// int LastOrderId = 111962758;
		// Integer OrderNum = 250;
		// List<Integer> OrderStatusList = new ArrayList<Integer>();
		// OrderStatusList.add(OrderStatusEnum.AccountPaid.getValue());
		// int SellerUserId = 20566120;
		// try {
		// List<Map> list = new YmtOrdersWapper().selectAppSellerOrders(
		// DateList, Filter, LastOrderId, OrderNum, OrderStatusList,
		// SellerUserId);
		// for (Map m : list) {
		// // System.out.println(m);
		// }
		// System.out.println(list.size());
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
		
		System.out.println(new YmtOrderInfoWapper().selectByOrderId(112482842));
	}
}
